﻿Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.CompilerServices
Imports System
Imports System.Collections
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Reflection
Imports System.Resources
Imports System.Security.Cryptography
Imports System.Net.Mail
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Runtime.CompilerServices

Namespace SV_Global.Global
    Public Class Funcoes

        ''' <summary>
        ''' Envia e-mails
        ''' </summary>
        ''' <param name="De"></param>
        ''' <param name="Para"></param>
        ''' <param name="Assunto"></param>
        ''' <param name="Corpo"></param>
        ''' <returns>Retorna True Em Caso De Sucesso, ou Exceção Em Caso De Erro</returns>
        ''' <remarks></remarks>
        Public Shared Function EnviaEmail(ByVal De As String, ByVal Para As String, ByVal Assunto As String, ByVal Corpo As String) As Object
            Try
                Dim Smtp = New SmtpClient
                Dim Mensagem As New MailMessage()
                With Mensagem
                    .From = New MailAddress(De)
                    .ReplyToList.Add(New MailAddress(Para))
                    .Subject = Assunto
                    .Body = Corpo
                End With
                Smtp.Host = SV_Global.Global.Configuracoes.ServidorSMTP
                Smtp.Send(Mensagem)
                Return True
            Catch ex As Exception
                Return ex
            End Try
        End Function

        ''' <summary>
        ''' Valida CNPJ
        ''' </summary>
        ''' <param name="CNPJ"></param>
        ''' <returns>Retorna Booleano</returns>
        ''' <remarks></remarks>
        Public Shared Function ValidaCNPJ(ByVal CNPJ As String) As Boolean
            Dim strArray As String() = New String() {"00000000000000", "11111111111111", "22222222222222", "33333333333333", "44444444444444", "55555555555555", "66666666666666", "77777777777777", "88888888888888", "99999999999999"}
            CNPJ = CNPJ.Trim
            CNPJ = CNPJ.Replace(".", "").Replace("/", "").Replace("-", "")
            If (CNPJ.Length < 14) Then
                Return False
            End If
            Dim str As String = CNPJ.Substring(0, 14)
            Dim num2 As Integer = (strArray.Length - 1)
            Dim i As Integer = 0
            Do While (i <= num2)
                If strArray(i).Equals(str) Then
                    Return False
                End If
                i += 1
            Loop
            Return BooleanType.FromObject(Funcoes.efetivaValidacao(CNPJ))
        End Function

        ''' <summary>
        ''' Finaliza Validação CNPJ
        ''' </summary>
        ''' <param name="cnpj"></param>
        ''' <returns>Retorna Objeto</returns>
        ''' <remarks></remarks>
        Private Shared Function efetivaValidacao(ByVal cnpj As String) As Object
            Dim num2 As Integer
            Dim num3 As Integer
            Dim numArray As Integer() = New Integer(14 - 1) {}
            Dim num5 As Integer = (numArray.Length - 1)
            Dim i As Integer = 0
            Do While (i <= num5)
                numArray(i) = IntegerType.FromString(cnpj.Substring(i, 1))
                i += 1
            Loop
            Dim num4 As Integer = ((((((((((((numArray(0) * 5) + (numArray(1) * 4)) + (numArray(2) * 3)) + (numArray(3) * 2)) + (numArray(4) * 9)) + (numArray(5) * 8)) + (numArray(6) * 7)) + (numArray(7) * 6)) + (numArray(8) * 5)) + (numArray(9) * 4)) + (numArray(10) * 3)) + (numArray(11) * 2))
            num4 = CInt(Math.Round(CDbl((num4 - (11 * Conversion.Int((CDbl(num4) / 11)))))))
            If ((num4 = 0) Or (num4 = 1)) Then
                num2 = 0
            Else
                num2 = (11 - num4)
            End If
            If (num2 <> numArray(12)) Then
                Return False
            End If
            num4 = (((((((((((((numArray(0) * 6) + (numArray(1) * 5)) + (numArray(2) * 4)) + (numArray(3) * 3)) + (numArray(4) * 2)) + (numArray(5) * 9)) + (numArray(6) * 8)) + (numArray(7) * 7)) + (numArray(8) * 6)) + (numArray(9) * 5)) + (numArray(10) * 4)) + (numArray(11) * 3)) + (numArray(12) * 2))
            num4 = CInt(Math.Round(CDbl((num4 - (11 * Conversion.Int((CDbl(num4) / 11)))))))
            If ((num4 = 0) Or (num4 = 1)) Then
                num3 = 0
            Else
                num3 = (11 - num4)
            End If
            Return (num3 = numArray(13))
        End Function

        ''' <summary>
        ''' Valida CPF
        ''' </summary>
        ''' <param name="CPF"></param>
        ''' <returns>Retorna Booleano</returns>
        ''' <remarks></remarks>
        Public Shared Function ValidaCPF(ByVal CPF As String) As Boolean
            Dim strArray As String() = New String() {"00000000000", "11111111111", "22222222222", "33333333333", "44444444444", "55555555555", "66666666666", "77777777777", "88888888888", "99999999999"}
            CPF = CPF.Trim
            CPF = CPF.Replace(".", "").Replace("-", "")
            If (CPF.Length < 11) Then
                Return False
            End If
            Dim str As String = CPF.Substring(0, 11)
            Dim num6 As Integer = (strArray.Length - 1)
            Dim index As Integer = 0
            Do While (index <= num6)
                If strArray(index).Equals(str) Then
                    Return False
                End If
                index += 1
            Loop
            Dim num4 As Integer = 0
            Do
                Dim num2 As Integer = 0
                Dim num5 As Integer = (8 + num4)
                index = 0
                Do While (index <= num5)
                    num2 = CInt(Math.Round(CDbl((num2 + (Conversion.Val(CPF.Substring(index, 1)) * ((10 + num4) - index))))))
                    index += 1
                Loop
                Dim num3 As Integer = CInt(Math.Round(CDbl((11 - (num2 - (Conversion.Int((CDbl(num2) / 11)) * 11))))))
                If ((num3 = 10) Or (num3 = 11)) Then
                    num3 = 0
                End If
                If (num3 <> Conversion.Val(CPF.Substring((9 + num4), 1))) Then
                    Return False
                End If
                num4 += 1
            Loop While (num4 <= 1)
            Return True
        End Function

        ''' <summary>
        ''' Valida Endereços De E-mail
        ''' </summary>
        ''' <param name="StrMail"></param>
        ''' <returns>Retorna Booleano</returns>
        ''' <remarks></remarks>
        Public Shared Function ValidaMail(ByVal StrMail As String) As Boolean
            Dim flag As Boolean
            Dim obj3 As New Object
            Dim obj4 As New Object
            Dim obj7 As New Object
            If (Strings.InStr(1, StrMail, " ", 0) > 0) Then
                Return False
            End If
            If (Strings.Len(RuntimeHelpers.GetObjectValue(Funcoes.FncStrSpace(StrMail))) < 6) Then
                Return False
            End If
            If (Strings.InStr(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), "@", 0) = 0) Then
                Return False
            End If
            If (Strings.InStr(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), ".", 0) = 0) Then
                Return False
            End If
            If ((Strings.Len(RuntimeHelpers.GetObjectValue(Funcoes.FncStrSpace(StrMail))) - Strings.InStrRev(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), ".", -1, 0)) > 3) Then
                Return False
            End If
            If ((Strings.InStr(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), "_", 0) <> 0) And (Strings.InStrRev(StrMail, "_", -1, 0) > Strings.InStrRev(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), "@", -1, 0))) Then
                Return False
            End If
            Dim obj2 As Object = 0
            If FlowControl.ForLoopInitObj(obj3, 1, Strings.Len(RuntimeHelpers.GetObjectValue(Funcoes.FncStrSpace(StrMail))), 1, obj7, obj3) Then
                Do
                    If (StringType.StrCmp(Strings.Mid(StrMail, IntegerType.FromObject(obj3), 1), "@", False) = 0) Then
                        obj2 = ObjectType.AddObj(obj2, 1)
                    End If
                Loop While FlowControl.ForNextCheckObj(obj3, obj7, obj3)
            End If
            If (ObjectType.ObjTst(obj2, 1, False) > 0) Then
                flag = True
            End If
            obj3 = 0
            If FlowControl.ForLoopInitObj(obj3, 1, Strings.Len(RuntimeHelpers.GetObjectValue(Funcoes.FncStrSpace(StrMail))), 1, obj4, obj3) Then
                Do
                    If ((((Not Information.IsNumeric(Strings.Mid(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), IntegerType.FromObject(obj3), 1)) And ((StringType.StrCmp(Strings.LCase(Strings.Mid(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), IntegerType.FromObject(obj3), 1)), "a", False) < 0) Or (StringType.StrCmp(Strings.LCase(Strings.Mid(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), IntegerType.FromObject(obj3), 1)), "z", False) > 0))) And (StringType.StrCmp(Strings.Mid(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), IntegerType.FromObject(obj3), 1), "_", False) <> 0)) And (StringType.StrCmp(Strings.Mid(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), IntegerType.FromObject(obj3), 1), ".", False) <> 0)) And (StringType.StrCmp(Strings.Mid(StringType.FromObject(Funcoes.FncStrSpace(StrMail)), IntegerType.FromObject(obj3), 1), "-", False) <> 0)) Then
                        flag = True
                    End If
                Loop While FlowControl.ForNextCheckObj(obj3, obj4, obj3)
            End If
            Return flag
        End Function

        Private Shared Function FncStrSpace(ByVal StrAddress As Object) As Object
            Return Strings.Trim(Strings.LTrim(Strings.RTrim(StringType.FromObject(StrAddress))))
        End Function

        Public Shared Function GeraHashSenha(ByVal Senha As String) As String
            Return FormsAuthentication.HashPasswordForStoringInConfigFile(Senha, "MD5")
        End Function

    End Class

End Namespace